'use strict';

const Service = require('egg').Service;

class Xyt_wuziService extends Service {
  async listAll() {
    // 数据库都是异步操作 await
    const result = await this.app.mysql.select('biz_supplier');
    return result;
  }
    //添加物资来源
    async addNewWuzi(biz_supplier) {
    // 数据库都是异步操作 await
    biz_supplier.create_time = this.app.mysql.literals.now;
    const result = await this.app.mysql.insert('biz_supplier', { ...biz_supplier });
    return result;
    // const querystring = 'INSERT into biz_supplier VALUES(?,?,?,?,?,NOW(),NOW(),?,?)';
    // const params = [ biz_supplier.id ];

    }
    //删除 物资来源
    async delwuzi(biz_supplier){
        const querystring = 'DELETE FROM biz_supplier WHERE id=?';
        const params = [ biz_supplier.id ];
        const result = await this.app.mysql.query(querystring, params);
        console.log(result)
        return result
    }
    //根据id获取某一条物资来源
    async updatehuoqu(biz_supplier){
        const querystring = 'SELECT * FROM `biz_supplier` WHERE id=?';
        const params = [biz_supplier.id];
        const result = await this.app.mysql.query(querystring, params);
        console.log(result);
        return result
    }
    //更改物资来源
    async updatewuzi(biz_supplier){
        const result = await this.app.mysql.update('biz_supplier', { ...biz_supplier });
        return result
    }
     //按条件查询用户
     async listwuzi(user) {
        let aa = Object.keys(user)[0]
        let bb = Object.keys(user)[1]
        let cc = Object.keys(user)[2]
        if (Object.keys(user).length === 1) {
            let queryString = 'select * from biz_supplier where ' + aa + ' like ?'
            let params = [user[aa] + '%'];
            console.log(params)
            let result = await this.app.mysql.query(queryString, params);
            return result;
        } else if (Object.keys(user).length === 2) {
            let queryString = 'select * from biz_supplier where ' + aa + ' like ? and ' + bb + ' like ?'
            let params = [user[aa] + '%', user[bb] + '%'];
            console.log(params)
            let result = await this.app.mysql.query(queryString, params);
            return result;
        } else if (Object.keys(user).length === 3) {
            let queryString = 'select * from biz_supplier where ' + aa + ' like ? and ' + bb + ' like ? and ' + cc + ' like ?'
            let params = [user[aa] + '%', user[bb] + '%', , user[cc] + '%'];
            let result = await this.app.mysql.query(queryString, params);
            return result;
        }
    }
}

module.exports = Xyt_wuziService;
